/*
 * Write a program that opens an existing file for writing with the O_APPEND flag, and
 * then seeks to the beginning of the file before writing some data. Where does the
 * data appear in the file? Why?
 */

// The file offset was positioned at the end of file

#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int
main (int argc, char *argv[])
{
  if (argc != 2) return EXIT_FAILURE;

  int fd;
  if ((fd = open (argv[1], O_WRONLY|O_APPEND)) == -1)
      return EXIT_FAILURE;

  off_t off;
  if ((off = lseek (fd, 0, SEEK_SET)) == -1)
    return EXIT_FAILURE;

  if ((write (fd, "test", 4)) == -1)
    return EXIT_FAILURE;

  close (fd);
  return EXIT_SUCCESS;
}
